package com.ycfmedical.bed.ui.fragment;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.xw.repo.BubbleSeekBar;
import com.ycfmedical.bed.Constants;
import com.ycfmedical.bed.ControlConstants;
import com.ycfmedical.bed.R;
import com.ycfmedical.bed.adapter.ControlImagePlay;
import com.ycfmedical.bed.bean.BedParameterInfo;
import com.ycfmedical.bed.bean.NurseControl;
import com.ycfmedical.bed.present.ControlPresenter;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.droidlover.xdroidmvp.event.BusProvider;
import cn.droidlover.xdroidmvp.log.XLog;
import cn.droidlover.xdroidmvp.mvp.XFragment;
import cn.droidlover.xdroidmvp.mvp.XLazyFragment;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017-03-22.
 * 体位控制_护理体位
 */
@SuppressLint("ValidFragment")
public class ControlnNurseFragment extends XLazyFragment<ControlPresenter> {

    @BindView(R.id.seek_nurse_stature)
    BubbleSeekBar seekNurseStature;
    @BindView(R.id.bed_slant_angle)
    BubbleSeekBar bedSlantAngle;
    @BindView(R.id.seek_container_two)
    LinearLayout seekContainerTwo;
    @BindView(R.id.seek_container_one)
    LinearLayout seekContainerOne;
    Unbinder unbinder;

    private int bottomNurseStature = 50;
    private int bottomSlantAngle = 15;

    public int getType() {
        return ControlConstants.CONTROL_NURSE;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        setTouchListener();
        _initBus();
        _initOntouch();
        if (Constants.bedId.equals("03"))
        {
            bedSlantAngle.setVisibility(View.INVISIBLE);//华信床屏蔽，德国床打开
        }
    }

    private void _initOntouch()
    {
        BusProvider.getBus().toObservableSticky(NurseControl.class)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe((NurseControl nurseControl) -> {
                    int lockState = nurseControl.getLockState();
                    if (lockState == 2)
                    {
                        Constants.lockState = "C2";
                        seekContainerOne.setVisibility(View.INVISIBLE);
                        seekContainerTwo.setVisibility(View.INVISIBLE);
                    }
                    else
                    {
                        Constants.lockState = "C8";
                        seekContainerOne.setVisibility(View.VISIBLE);
                        seekContainerTwo.setVisibility(View.VISIBLE);
                    }
                });
    }

    private void _initBus()
    {
        BusProvider.getBus().toObservableSticky(BedParameterInfo.class)
                .subscribeOn(Schedulers.io())
                .sample(2500, TimeUnit.MILLISECONDS)
                .onBackpressureDrop()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BedParameterInfo>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        request(1);
                    }

                    @Override
                    public void onCompleted() {
                        if (!isUnsubscribed()) {
                            this.unsubscribe();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        XLog.e(getTag(),"" + e);
                    }

                    @Override
                    public void onNext(BedParameterInfo bedParameterInfo) {
                        bottomNurseStature = bedParameterInfo.getNurseStature();
                        bottomSlantAngle = bedParameterInfo.getSlantAngle();
                        setSeekbarParameter(bottomSlantAngle, bottomNurseStature);
                        request(1);
                    }
                });
    }


    @Override
    public void onStart() {
        super.onStart();
    }

    private void setTouchListener()
    {
        //设置倾斜角度
        bedSlantAngle.setOnProgressChangedListener(slantAngleListener);
        //设置护士身高
        seekNurseStature.setOnProgressChangedListener(nurseStatureListener);
    }


    public BubbleSeekBar.OnProgressChangedListener slantAngleListener = new BubbleSeekBar.OnProgressChangedListener() {

        @Override
        public void onProgressChanged(int progress, float progressFloat) {

        }

        @Override
        public void getProgressOnActionUp(int progress, float progressFloat) {

            int SlantAngle = calculateSlantAngle(bottomSlantAngle);

            if (progress  - SlantAngle < 0)
            {
                bedSlantAngle.setProgress(SlantAngle);
            }
            else if (progress - SlantAngle > 0)
            {
                getP().sendSlantAngleProgress(progress + 15);
                bedSlantAngle.setProgress(progress);
                bottomSlantAngle = progress+15;
            }


        }

        private int calculateSlantAngle(int bottomSlantAngle) {
            int SlantAngle = 0;
            if (bottomSlantAngle < 0) {
                SlantAngle = -15;
            } else if (30 < bottomSlantAngle) {
                SlantAngle = 15;
            } else {
                SlantAngle = bottomSlantAngle - 15;
            }
            return SlantAngle;
        }

        @Override
        public void getProgressOnFinally(int progress, float progressFloat) {

        }
    };

    public BubbleSeekBar.OnProgressChangedListener nurseStatureListener = new BubbleSeekBar.OnProgressChangedListener() {

        @Override
        public void onProgressChanged(int progress, float progressFloat) {

        }

        @Override
        public void getProgressOnActionUp(int progress, float progressFloat)
        {
            XLog.e("TZT",progress+"");
            getP().SendStatureProgress(progress);
        }

        @Override
        public void getProgressOnFinally(int progress, float progressFloat) {

        }
    };

    @Override
    public int getLayoutId() {
        return R.layout.fragment_control_nurse;
    }

    @Override
    public ControlPresenter newP() {
        return new ControlPresenter();
    }

    public static ControlnNurseFragment newInstance() {
        return new ControlnNurseFragment();
    }

    private void setSeekbarParameter(int bottomSlantAngle, int bottomNurseStature) {
        //设置倾斜角度
        bedSlantAngle.setProgress(bottomSlantAngle - 15);
        //设置床高
        seekNurseStature.setProgress(bottomNurseStature);
    }
}
